package com.newear;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.newear.activity.HappyMainActivity;
import com.newear.util.MyMediaPlayer;

import java.lang.ref.WeakReference;

/**
 * 启动页面
 * Created by josan_tang on 2015/8/16.
 */
public class SplashActivity extends Activity {
    private static final String TAG = "SplashActivity";

    private static final int UPDATE_TIME = 5;

    private ImageView ivSplash;
    private TextView tvTime;

    private TextView tvSkip;

    private int adTime = 5;

    private MediaPlayer mediaPlayer;
    private MyHandler mHandler;
    private boolean goHome = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        init();
        findViews();
        setListeners();
    }

    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void findViews() {
        ivSplash = (ImageView) this.findViewById(R.id.iv_splash);
        tvTime = (TextView) this.findViewById(R.id.tv_time);
        tvSkip = (TextView) this.findViewById(R.id.tv_skip);
        ivSplash.setBackgroundResource(R.drawable.splash);
    }

    /**
     * 设置监听
     */
    private void setListeners() {
        tvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!goHome) {
                    goHome();
                }
            }
        });
        ivSplash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!goHome) {
                    goHome();
                }
            }
        });
    }

    /**
     * 初始化方法
     */
    private void init() {
        mHandler = new MyHandler(SplashActivity.this);
        mHandler.sendEmptyMessageDelayed(UPDATE_TIME, 1000);
        //mediaPlayer = MyMediaPlayer.getInstance().playing(this, "raw/splash.mp3");
        mediaPlayer = MyMediaPlayer.getInstance().playing2(this, R.raw.splash);
    }

    /**
     * 跳到主页
     */
    private void goHome() {
        goHome = true;
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }
        mediaPlayer = null;
        Intent intent = new Intent(SplashActivity.this, HappyMainActivity.class);
        startActivity(intent);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 500);
    }

    static class MyHandler extends Handler {
        WeakReference<Activity> mActivityReference;

        MyHandler(Activity activity) {
            mActivityReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            try {
                final SplashActivity activity = (SplashActivity) mActivityReference.get();
                if (activity != null) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                        case UPDATE_TIME:
                            if (activity.goHome) {
                                return;
                            }
                            activity.adTime--;
                            if (activity.adTime >= 0) {
                                activity.tvTime.setText(activity.adTime + "s");
                                if (activity.adTime == 0) {
                                    activity.goHome();
                                } else if (activity.adTime > 0) {
                                    activity.mHandler.sendEmptyMessageDelayed(activity.UPDATE_TIME, 1000);
                                }
                            } else {
                                activity.goHome();
                            }
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
